package ua.com.infinite.mobile.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;

import ua.com.infinite.mobile.App;
import ua.com.infinite.mobile.LoginActivity;
import ua.com.infinite.mobile.R;
import ua.com.infinite.mobile.server.Constants;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

public class PhoneUtils {
	
	public static boolean isInternetConnected(final Context context) {
        boolean connected = false;

        ConnectivityManager cm = 
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (cm != null) {
            NetworkInfo[] netInfo = cm.getAllNetworkInfo();

            for (NetworkInfo ni : netInfo) {
                if ((ni.getTypeName().equalsIgnoreCase("WIFI")
                        || ni.getTypeName().equalsIgnoreCase("MOBILE"))
                        && ni.isConnected() && ni.isAvailable()) {
                    connected = true;
                }
            }
        }
        return connected;
    } 

	public static String getDevicePhoneNumber(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String number = telephonyManager.getLine1Number();
		return number;
	}

	public static boolean isValidPhoneNumber(String number) {
		Pattern pattern = Pattern.compile("\\d{12}");
		Matcher matcher = pattern.matcher(number);

		return matcher.matches();
	}
	
	public static boolean isEmailValid(String address){
		Pattern pattern = Pattern.compile("([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})");
		Matcher matcher = pattern.matcher(address);

		return matcher.matches();
	}
	
	public static boolean isNameValid(String name){
		Pattern pattern = Pattern.compile("[a-zA-Z]+");
		Matcher matcher = pattern.matcher(name);

		return matcher.matches();
	}
	
	public static boolean isJSonValid(JSONObject object){
		boolean result = false;
		if (object == null) {
			return false;
		}
		int error_id;
		try {
			error_id = object.getInt(Constants.ERROR_ID);
			if (error_id == 0) {
				result = true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}finally{
			return result;
		}
	}
	
	public static int getColorByStatus(Context context,int statusId){
		switch (statusId) {
		case 1:
			return context.getResources().getColor(R.color.status1);
		case 2:
			return context.getResources().getColor(R.color.status2);
		case 3:
			return context.getResources().getColor(R.color.status3);
		default:
			return context.getResources().getColor(android.R.color.white);
		}
	}
	
	
}
